package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.IWidgetBuilder;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.model.PaginationModel;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.model.DisplayModel;
import com.dsteiner.xelavos.client.model.MyEntriesModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class MyEntriesController {

	private MyEntriesModel myEntriesModel;
	private IWidgetBuilder widgetBuilder;
	private IFlowPanel contentPanel;
	private PaginationModel paginationModel;
	private ILazyWidget myEntriesWidget;
	private PopupOverlayModel popupOvelayModel;

	public void registerContentPanel(IFlowPanel contentPanel) {
		this.contentPanel = contentPanel;
	}

	public void registerMyEntriesModel(final MyEntriesModel myEntriesModel) {
		this.myEntriesModel = myEntriesModel;
		myEntriesModel.addPropertyChangeListener("modelObjects", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateEntries();
			}
		});
		myEntriesModel.addPropertyChangeListener("count", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				paginationModel.setCount(myEntriesModel.getCount());
			}
		});
	}

	public void registerMyEntriesWidget(ILazyWidget myEntriesWidget) {
		this.myEntriesWidget = myEntriesWidget;
		myEntriesWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateEntries();
				updateOffsetLimit();
				myEntriesModel.setAttached(true);
			}
			@Override
			public void onDetach(IWidget widget) {
				myEntriesModel.setAttached(false);
			}
		});
	}

	public void registerPaginationModel(final PaginationModel paginationModel) {
		this.paginationModel = paginationModel;
		paginationModel.addPropertyChangeListener("itemsPerPage", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateOffsetLimit();
			}
		});
		paginationModel.addPropertyChangeListener("page", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateOffsetLimit();
			}
		});
	}

	public void registerWidgetBuilder(IWidgetBuilder widgetBuilder) {
		this.widgetBuilder = widgetBuilder;
	}

	private void updateEntries() {
		contentPanel.clear();
		List<ModelObject> modelObjects = myEntriesModel.getModelObjects();
		if(modelObjects == null)
			return;
		
		for (ModelObject modelObject : modelObjects) {
			DisplayModel displayModel = new DisplayModel();
			displayModel.setModelObject(modelObject);
			IWidget widget = widgetBuilder.createWidget(displayModel);
			contentPanel.add(widget);
		}
//		EventManagerImpl.getInstance().fireEvent(new WidgetSizeChangeEvent((Widget) myEntriesWidget));
	}

	private void updateOffsetLimit() {
		myEntriesModel.setLimit(paginationModel.getItemsPerPage());
		myEntriesModel.setOffset(paginationModel.getOffset());
	}

	public void registerCloseLink(IHTML closeLink) {
		closeLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				popupOvelayModel.setVisible(false);
			}
		});
	}

	public void registerPopupOverlayModel(PopupOverlayModel popupOvelayModel) {
		this.popupOvelayModel = popupOvelayModel;
	}
}